home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / ipc / msgtool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-01  |  2.9 KB  |  123 lines

  1. /*****************************************************************************
  2.  Excerpt from "Linux Programmer's Guide - Chapter 6"
  3.  (C)opyright 1994-1995, Scott Burkett
  4.  ***************************************************************************** 
  5.  MODULE: msgtool.c
  6.  *****************************************************************************
  7.  A command line tool for tinkering with SysV style Message Queues
  8.  *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <sys/ipc.h>
  15. #include <sys/msg.h>
  16.  
  17. #define MAX_SEND_SIZE 80
  18.  
  19. struct mymsgbuf {
  20.     long mtype;
  21.     char mtext[MAX_SEND_SIZE];
  22. };
  23.  
  24. void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text);
  25. void read_message(int qid, struct mymsgbuf *qbuf, long type);
  26. void remove_queue(int qid);
  27. void change_queue_mode(int qid, char *mode);
  28. void usage(void);
  29.  
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.     key_t key;
  34.     int   msgqueue_id;
  35.     struct mymsgbuf qbuf;
  36.  
  37.     if(argc == 1)
  38.         usage();
  39.  
  40.     /* Create unique key via call to ftok() */
  41.     key = ftok(".", 'm');
  42.  
  43.     /* Open the queue - create if necessary */
  44.     if((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
  45.         perror("msgget");
  46.         exit(1);
  47.     }
  48.     
  49.     switch(tolower(argv[1][0]))
  50.     {
  51.         case 's': send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,
  52.                        atol(argv[2]), argv[3]); 
  53.               break;
  54.         case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2])); 
  55.               break;
  56.         case 'd': remove_queue(msgqueue_id); 
  57.               break;    
  58.         case 'm': change_queue_mode(msgqueue_id, argv[2]); 
  59.               break;
  60.  
  61.          default: usage();
  62.  
  63.     }
  64.     
  65.     return(0);
  66. }
  67.  
  68. void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
  69. {
  70.      /* Send a message to the queue */
  71.     printf("Sending a message ...\n");
  72.     qbuf->mtype = type;
  73.     strcpy(qbuf->mtext, text);
  74.  
  75.     if((msgsnd(qid, (struct msgbuf *)qbuf,
  76.         strlen(qbuf->mtext)+1, 0)) ==-1)
  77.     {
  78.         perror("msgsnd");
  79.         exit(1);
  80.     }
  81. }
  82.  
  83. void read_message(int qid, struct mymsgbuf *qbuf, long type)
  84. {
  85.     /* Read a message from the queue */
  86.     printf("Reading a message ...\n");
  87.     qbuf->mtype = type;
  88.     msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
  89.     
  90.     printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
  91. }
  92.  
  93. void remove_queue(int qid)
  94. {
  95.     /* Remove the queue */
  96.     msgctl(qid, IPC_RMID, 0);
  97. }
  98.  
  99. void change_queue_mode(int qid, char *mode)
  100. {
  101.     struct msqid_ds myqueue_ds;
  102.  
  103.     /* Get current info */
  104.     msgctl(qid, IPC_STAT, &myqueue_ds);
  105.  
  106.     /* Convert and load the mode */
  107.     sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
  108.  
  109.     /* Update the mode */
  110.     msgctl(qid, IPC_SET, &myqueue_ds);
  111. }
  112.  
  113. void usage(void)
  114. {
  115.     fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
  116.     fprintf(stderr, "\nUSAGE: msgtool (s)end <type> <messagetext>\n");
  117.     fprintf(stderr, "               (r)ecv <type>\n");
  118.     fprintf(stderr, "               (d)elete\n");
  119.     fprintf(stderr, "               (m)ode <octal mode>\n");
  120.     exit(1);
  121. }
  122.  
  123.